package com.hwg.emp.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hwg.emp.common.util.DBUtil;
import com.hwg.emp.common.util.ThisAppUtil;
import com.hwg.emp.dto.OvertimeDto;
import com.hwg.emp.model.UserMD;
import com.hwg.emp.service.OvertimeService;
import com.hwg.emp.service.ServiceFactroy;
import com.hwg.emp.service.UserService;

public class AddEmpOvertimeController extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		OvertimeService overtimeService=ServiceFactroy.getService(OvertimeService.class);
		UserService userService=ServiceFactroy.getService(UserService.class);
		String account=req.getParameter("addaccount");
		String startdate=req.getParameter("addstartdate");
		String days=req.getParameter("adddays");
		String reason=req.getParameter("addreason");
		java.sql.Date currentStartDate=java.sql.Date.valueOf(startdate);
		int currentDays=ThisAppUtil.parseInt(days, null);
		OvertimeDto overtimeDto=new OvertimeDto();
		try {
			UserMD user = userService.queryUserByAccount(account);
			overtimeDto.setId(DBUtil.uuid());
			overtimeDto.setStartDate(currentStartDate);
			overtimeDto.setDays(currentDays);
			overtimeDto.setReason(reason);
			overtimeDto.setUser(user);
			boolean flag=overtimeService.addEmpOvertime(overtimeDto);
			if(flag){
				req.getRequestDispatcher("/selectEmpOvertimes.do").forward(req, resp);
			}
			else{
				req.setAttribute("erro", "对不起，添加失败");
				req.getRequestDispatcher("/selectEmpOvertimes.do").forward(req, resp);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
